package com.hd.chat.widget;

import android.content.Context;
import android.content.DialogInterface;

import androidx.appcompat.app.AlertDialog;

/**
 * Created by Administrator on 2018/7/5.
 */

public class CDialog {

    private AlertDialog dialogBuilder;

    private CDialog(final Builder builder) {
        dialogBuilder = new AlertDialog.Builder(builder.context)
                .setTitle(builder.title)
                .setMessage(builder.message)
                .setCancelable(builder.cancelable)
                .setPositiveButton(builder.confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (builder.onConfirmListener != null)
                            builder.onConfirmListener.onClick(dialog, which);
                        dialog.dismiss();
                    }
                })
                .setNegativeButton(builder.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (builder.onCancelListener != null)
                            builder.onCancelListener.onClick(dialog, which);
                        dialog.dismiss();
                    }
                }).create();
    }

    public void show() {
        if (dialogBuilder != null) dialogBuilder.show();
    }

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String cancel;
        private String confirm;
        private DialogInterface.OnClickListener onCancelListener;
        private DialogInterface.OnClickListener onConfirmListener;
        private boolean cancelable = true;

        public Builder(Context context) {
            this.context = context;
            title = "提示";
            message = "";
            confirm = "确定";
        }

        public Builder setCancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setTitle(int titleRes) {
            this.title = context.getResources().getString(titleRes);
            return this;
        }

        public Builder setMessage(int messageRes) {
            this.message = context.getResources().getString(messageRes);
            return this;
        }

        public Builder setCancel(String cancel) {
            this.cancel = cancel;
            return this;
        }

        public Builder setCancel(int cancel) {
            this.cancel = context.getResources().getString(cancel);
            return this;
        }

        public Builder setCancel(String cancel, DialogInterface.OnClickListener onCancelListener) {
            this.onCancelListener = onCancelListener;
            this.cancel = cancel;
            return this;
        }

        public Builder setCancel(int cancel, DialogInterface.OnClickListener onCancelListener) {
            this.onCancelListener = onCancelListener;
            this.cancel = context.getResources().getString(cancel);
            return this;
        }

        public Builder setConfirm(String confirm, DialogInterface.OnClickListener onConfirmListener) {
            this.onConfirmListener = onConfirmListener;
            this.confirm = confirm;
            return this;
        }

        public Builder setConfirm(int confirm, DialogInterface.OnClickListener onConfirmListener) {
            this.onConfirmListener = onConfirmListener;
            this.confirm = context.getResources().getString(confirm);
            return this;
        }

        public CDialog build(){
            return new CDialog(this);
        }
    }
}
